/// File: Robotics4.NET\BodyMap\Messages.cs
/// 
/// ------------------------------------------------------------
/// Copyright (c) 2004
///   Antonio Cisternino (cisterni.unipi.it),
///   Diego Colombo      (colombod.unipi.it)
/// 
/// The use and distribution terms for this software are 
/// contained in the file named license.txt, which can be found 
/// in the root of this distribution.
/// By using this software in any fashion, you are agreeing to 
/// be bound by the terms of this license.
///
/// You must not remove this notice, or any other, from this
/// software.
/// ------------------------------------------------------------

using System;
using System.Xml.Serialization;

namespace Robotics4.NET
{
    /// <summary>
    /// Base class for roblet messages
    /// </summary>
    public abstract class RobletMessage
    {
        public enum PacketType
        {
            Data,
            UrgentData,
            Problem,
            CriticalProblem,
            Exception
        }
        public string Name;
        public int RobletID;
        public string BrokerHost;
        public int BrokerPort;
        public PacketType Type;
        public RobletMessage()
        {
            RobletID = -1;
            Type = PacketType.Data;
        }
        public void CopyDeliveryInfo(RobletMessage src)
        {
            Name = src.Name;
            RobletID = src.RobletID;
            BrokerHost = src.BrokerHost;
            BrokerPort = src.BrokerPort;
            Type = src.Type;
        }
        public bool ContainsDeliveryInfo
        {
            get
            {
                return RobletID != -1;
            }
        }
    }

    public abstract class Problem : RobletMessage
    {
        public Problem()
        {
            Type = PacketType.Problem;
        }
    }

    public class RobletInterface : RobletMessage
    {
        public bool IsSticklet = false;
        [XmlArray(ElementName = "InputMessages", IsNullable = true)]
        public InterfaceEntry[] InputMessages;
        [XmlArray(ElementName = "OutputMessages", IsNullable = true)]
        public InterfaceEntry[] OutputMessages;
        [XmlArray(ElementName = "Friends", IsNullable = true)]
        public string[] Friends;
    }

    public class RobletDead : RobletMessage
    {
        public RobletDead()
        {
            Type = PacketType.CriticalProblem;
        }
    }

    [Serializable]
    public class RobletException : RobletMessage
    {
        public string Exception;
#if WIN
        public string StackTrace;
#endif

        public RobletException()
        {
            Type = PacketType.Exception;
        }

        public RobletException(Exception e)
        {
            Exception = e.Message;
#if WIN
            StackTrace = e.StackTrace;
#endif
            Type = PacketType.Exception;
        }
    }

    public class MetaDataRequest : RobletMessage
    {
        public MetaDataRequest()
        {
            Type = PacketType.Data;
        }
    }

    public class FriendInfo : RobletMessage
    {
        public string FriendBrokerHost;
        public int FriendBrokerPort;
        public int FriendRobletID;
        public string FriendRobletName;

        public FriendInfo()
        {
            Type = PacketType.Data;
        }
    }

    public class RestartRoblet : RobletMessage
    {
        public RestartRoblet()
        {
            Type = PacketType.UrgentData;
        }
    }

    public enum StickletRequest
    {
        StickletList,
        Attach,
        Detach,
        Detached
    }

    public class StickletMessage : RobletMessage
    {
        public StickletRequest RequestType;
        public string StickletName;

        public StickletMessage()
        {
            Type = PacketType.UrgentData;
        }
    }

    public class StickletBrokerMessage : RobletMessage
    {
        public string[] AvaliableSticklets;

        public StickletBrokerMessage()
        {
            Type = PacketType.UrgentData;
            AvaliableSticklets = new string[0];
        }
    }

    public class HeartBeatMessage : RobletMessage
    {
        public HeartBeatMessage()
        {
            Type = PacketType.Data;
        }
    }
}